<?php
/**
 * Created by PhpStorm.
 * User: 顾明轩
 * Date: 2017/10/2
 * Time: 23:02
 */

namespace Home\Model;


use Think\Model;

class Alipay extends Model
{
    var $aop;
    /**
     * 初始化
     */
    protected function _initialize()
    {
        //parent::_initialize();
        //支付宝支付
        require_once(join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), 'lib\Alipay\aop', 'AopClient.php')));
        $this->aop    =    new \AopClient();
        $this->aop->gatewayUrl             = C('Alipay')['alipay_gateway'];
        $this->aop->appId                 = C('Alipay')['alipay_appId'];
        $this->aop->gatewayUrlC             =C('Alipay')['alipay_gateway'];
        $this->aop->format                 = "json";
        $this->aop->charset                = "utf-8";
        $this->aop->signType            = "RSA2";
        $this->aop->alipayrsaPublicKey     = C('Alipay')['alipay_rsaPublicKey'];//支付宝共有密钥
        $this->aop->rsaPrivateKey         = C('Alipay')['alipay_rsaPrivateKey'];//开发者私有密钥
        //$this->aop->encryptKey          =C('Alipay')['alipay_encryptKey'];
    }



    public function queryAppPay($out_trade_no,$trade_no = ""){


        require_once(join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), 'lib\Alipay\aop\request', 'AlipayTradeQueryRequest.php')));
        $request = new \AlipayTradeQueryRequest();
        $request->setBizContent(json_encode(array(
            'out_trade_no'=>$out_trade_no,
            'trade_no'=>$trade_no
        )));

        $result = $this->aop->execute($request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";

        $resultCode = $result->$responseNode->code;


        if(!empty($resultCode)&&$resultCode == 10000){
            return true;
        } else {
            return false;
        }
    }

    public function refund(){

        require_once(join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)), 'lib\Alipay\aop\request', 'AlipayTradeQueryRequest.php')));
        $request = new \AlipayTradeRefundRequest();
        $request->setBizContent("{" .
            "\"out_trade_no\":\"20150320010101001\"," .
            "\"trade_no\":\"2014112611001004680073956707\"," .
            "\"refund_amount\":200.12," .
            "\"refund_reason\":\"正常退款\"," .
            "\"out_request_no\":\"HZ01RF001\"," .
            "\"operator_id\":\"OP001\"," .
            "\"store_id\":\"NJ_S_001\"," .
            "\"terminal_id\":\"NJ_T_001\"" .
            "  }");
        $result = $aop->execute ( $request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失败";
        }
    }


    /**
     * 异步通知验签
     *
     * @param string $params 参数
     * @param string $signType 签名类型：默认RSA
     * @return bool 是否通过
     */
    public function rsaCheck($params, $signType)
    {
        return $this->aop->rsaCheckV1($params, NULL, $signType);
    }
}